package cn.broadmax.boot.modules.sys.service.impl;

import cn.broadmax.boot.log.LogRecord;
import cn.broadmax.boot.log.LogService;
import cn.broadmax.boot.modules.sys.entity.SysUser;
import cn.broadmax.boot.modules.sys.entity.SysLog;
import cn.broadmax.boot.modules.sys.mapper.SysLogMapper;
import cn.broadmax.boot.modules.sys.service.ISysLogService;
import cn.broadmax.boot.modules.sys.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jexx.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author jexx
 * @since 2021-05-17
 */
@Service
@RequiredArgsConstructor
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService, LogService {

    private final ISysUserService sysUserService;

    @Override
    public boolean saveLog(LogRecord log) {
        SysLog sysLog = new SysLog();
        sysLog.setLogType(log.getLogType().getType());
        sysLog.setLogContent(log.getLogContent());
        String userId = log.getUserId();
        if(StringUtil.hasText(userId)){
            sysLog.setUserid(userId);
            SysUser sysUser = sysUserService.getById(userId);
            if(sysUser != null){
                sysLog.setUsername(sysUser.getUsername());
            }
        }
        sysLog.setIp(log.getIp());
        sysLog.setMethod(log.getMethod());
        sysLog.setRequestUrl(log.getRequestUrl());
        sysLog.setRequestType(log.getRequestType());
        sysLog.setLogTime(log.getLogTime());
        sysLog.setCostTime(log.getCostTime());
        super.save(sysLog);
        return true;
    }

}
